package com.hskn.hss.module.notice.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hskn.hss.core.utils.LoginUtils;
import com.hskn.hss.exception.HssException;
import com.hskn.hss.module.notice.entity.Notice;
import com.hskn.hss.module.notice.mapper.NoticeMapper;
import com.hskn.hss.module.notice.service.INoticeService;
import com.hskn.hss.utils.StringUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author hskn
 * @since 2021-04-30
 */
@Service
public class NoticeServiceImpl extends ServiceImpl<NoticeMapper, Notice> implements INoticeService {

    @Resource
    NoticeMapper noticeMapper;

    @Override
    public JSONObject mList(JSONObject params) throws HssException {
        String createUid = params.getString("createUid");
        Long page = StringUtils.isEmpty(params.getLong("page")) ? 1 : params.getLong("page");
        Long size = StringUtils.isEmpty(params.getLong("size")) ? 10 : params.getLong("size");
        String startDate = params.getString("startDate");
        String endDate = params.getString("endDate");
        JSONObject json = new JSONObject();
        QueryWrapper<Notice> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().orderByDesc(Notice::getCreateTime);
        queryWrapper.lambda().between(Notice::getCreateTime, startDate, endDate);
        if (StringUtils.isNotEmpty(createUid)) {
            String[] createUidList = createUid.split(",");
            queryWrapper.lambda().in(Notice::getCreateUid, createUidList);
        }
        Page<Notice> noticePage = new Page<>(page, size, true);
        IPage<Notice> iPage = noticeMapper.selectPage(noticePage, queryWrapper);
        json.put("total", iPage.getTotal());
        json.put("size", iPage.getSize());
        json.put("pages", iPage.getPages());
        json.put("current", iPage.getCurrent());
        json.put("notices", iPage.getRecords());
        return json;
    }

    @Override
    public JSONObject list4app(JSONObject params) throws HssException {

        String uid = LoginUtils.getUser().getUid();
        Long page = StringUtils.isEmpty(params.getLong("page")) ? 1 : params.getLong("page");
        Long size = StringUtils.isEmpty(params.getLong("size")) ? 10 : params.getLong("size");
        String startDate = params.getString("startDate");
        String endDate = params.getString("endDate");
        JSONObject json = new JSONObject();
        QueryWrapper<Notice> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().orderByDesc(Notice::getCreateTime);
        if (startDate != null && !startDate.equals("") && endDate != null && !endDate.equals("")) {
            queryWrapper.lambda().between(Notice::getCreateTime, startDate, endDate);
        }
        queryWrapper.lambda().like(Notice::getReceiver, uid);
        Page<Notice> noticePage = new Page<>(page, size, true);
        IPage<Notice> iPage = noticeMapper.selectPage(noticePage, queryWrapper);
        if(page-iPage.getPages()<=0){
            json.put("total", iPage.getTotal());
            json.put("size", iPage.getSize());
            json.put("pages", iPage.getPages());
            json.put("current", iPage.getCurrent());
            json.put("notices", iPage.getRecords());
        }
        return json;
    }

    @Override
    public Notice getDetailById(JSONObject params) throws HssException {

        String uid = LoginUtils.getUser().getUid();
        String noticeId = params.getString("noticeId");
        Notice notice = noticeMapper.selectById(noticeId);
        if (StringUtils.isEmpty(notice)) {
            throw new HssException("内容不存在");
        }
        String[] rUids = notice.getReceiver().split(",");
        if (notice.getCreateUid().equals(uid) || ArrayUtils.contains(rUids, uid)) {
            return notice;
        } else {
            throw new HssException("越权请求！");
        }
    }

    @Override
    public void add(JSONObject params) throws HssException {

        String uid = LoginUtils.getUser().getUid();
        Notice notice = JSON.parseObject(params.toJSONString(), Notice.class);
        notice.setCreateUid(uid);
        noticeMapper.insert(notice);
        // TODO: 2021/4/30 推送通知到接收人 app
    }

    @Override
    public void mUpdate(JSONObject params) throws HssException {

        String uid = LoginUtils.getUser().getUid();
        Notice notice = JSON.parseObject(params.toJSONString(), Notice.class);
        Notice currNotice = noticeMapper.selectById(notice.getNoticeId());
        if (StringUtils.isEmpty(currNotice)) {
            throw new HssException("不存在的通知");
        }
        if (currNotice.getCreateUid().equals(uid)) {
            throw new HssException("请勿修改非本人发布的通知");
        }
        noticeMapper.updateById(notice);
    }


    @Override
    public void del(JSONObject params) throws HssException {

        String[] noticeIds = params.getString("noticeIds").split(",");
        noticeMapper.deleteBatchIds(Arrays.asList(noticeIds));
        // TODO: 2021/4/30 判断越权
    }
}
